package cn.cqie.attendance.teacher.service.impl;

import cn.cqie.attendance.common.util.LocalJwt;
import cn.cqie.attendance.common.util.Page;
import cn.cqie.attendance.common.util.PageBean;
import cn.cqie.attendance.teacher.dao.TeacherScoreDao;
import cn.cqie.attendance.teacher.pojo.TeacherHistoryTask;
import cn.cqie.attendance.teacher.pojo.TeacherScore;
import cn.cqie.attendance.teacher.service.TeacherScoreService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wqh
 * @date 2021/6/22 14:39
 */
@Service
public class TeacherScoreServiceImpl implements TeacherScoreService {

    @Autowired
    private TeacherScoreDao teacherScoreDao;
    @Autowired
    private HttpServletRequest request;

    //int teacherId = 1;
    @Override
    public PageBean<TeacherScore> queryCoursePersonByTeacherId(Integer courseId, Integer courseGroupId, Integer pageNum, Integer pageSize) {
        //当前登录教师id
        int teacherId = LocalJwt.getCurrentUserId(request);
        if (pageNum==null || pageSize==null){
            pageNum = 1;
            pageSize = 10;
        }
        PageBean pageBean = new PageBean();
        pageBean.setPageNum(pageNum);
        pageBean.setPageSize(pageSize);
        PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        Map<String,Object> map = new HashMap<>();
        map.put("teacherId",teacherId);
        map.put("courseId",courseId);
        map.put("courseGroupId",courseGroupId);
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        List<TeacherScore> teacherScores = teacherScoreDao.queryCoursePersonByTeacherId(map);
        int total = teacherScores.size();
        if (null == teacherScores || total == 0) {
            return  new PageBean<>(200, "查询列表为空");
        }
        return new PageBean<>(200, "Success",teacherScores);
    }

    @Override
    public PageBean<TeacherScore> queryCourseGroupByTeacherId(Integer courseId, Integer groupId, Integer pageNum, Integer pageSize) {
        //当前登录教师id
        int teacherId = LocalJwt.getCurrentUserId(request);
        if (pageNum==null || pageSize==null){
            pageNum = 1;
            pageSize = 10;
        }
        PageBean pageBean = new PageBean();
        pageBean.setPageNum(pageNum);
        pageBean.setPageSize(pageSize);
        PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        Map<String,Object> map = new HashMap<>();
        map.put("teacherId",teacherId);
        map.put("courseId",courseId);
        map.put("groupId",groupId);
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        List<TeacherScore> teacherScoreList = teacherScoreDao.queryCourseGroupByTeacherId(map);
        int total = teacherScoreList.size();

        if (null == teacherScoreList || total == 0) {
            return  new PageBean<>(200, "查询列表为空");
        }
        return new PageBean<>(200, "Success", teacherScoreList);
    }

    @Override
    public Page<TeacherScore> queryCourseGroupDetailByGroupId(Integer groupId) {
      List<TeacherScore> scoreList = teacherScoreDao.queryCourseGroupDetailByGroupId(groupId);
        if (scoreList == null){
            return Page.FAIL;
        }else {
            return new Page<>(200, "Success", scoreList);
        }
    }

    @Override
    public Page queryPersonTaskByStudentId(Integer courseId, Integer studentId) {
        Map<String,Object> map = new HashMap<>();
        //map.put("teacherId",teacherId);
        map.put("courseId",courseId);
        map.put("studentId",studentId);
        List<TeacherScore> teacherScore = teacherScoreDao.queryPersonTaskByStudentId(map);
        if (teacherScore == null){
            return Page.FAIL;
        }else {
            return new Page(200,"success",teacherScore);
        }
    }

    @Override
    public Page updateStudentCourseByStudentId(float overAllScore, String comment, Integer courseId, Integer studentId,Integer responseTeacher) {
        //当前登录教师id
        int teacherId = LocalJwt.getCurrentUserId(request);
        if (teacherId == responseTeacher){
            Date submitTime = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Map<String,Object> map = new HashMap<>();
            map.put("overAllScore",overAllScore);
            map.put("comment",comment);
            map.put("courseId",courseId);
            map.put("stuId",studentId);
            map.put("submitTime",sdf.format(submitTime));
            int count = teacherScoreDao.updateStudentCourseByStudentId(map);
            if (count > 0){
                return Page.SUCCESS;
            }else {
                return Page.FAIL;
            }
        }else {
            return new Page(201,"当前登录用户没有该权限");
        }
    }

    @Override
    public PageBean<TeacherHistoryTask> queryHistoryTaskByStudentId(Integer studentId, Integer courseId, Integer pageNum, Integer pageSize) {
        if (pageNum==null || pageSize==null){
            pageNum = 1;
            pageSize = 10;
        }
        PageBean pageBean = new PageBean();
        pageBean.setPageNum(pageNum);
        pageBean.setPageSize(pageSize);
        PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        Map<String,Object> map = new HashMap<>();
        map.put("studentId",studentId);
        map.put("courseId",courseId);
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        List<TeacherHistoryTask> teacherHistoryTasks = teacherScoreDao.queryHistoryTaskByStudentId(map);
        int total = teacherHistoryTasks.size();

        if (null == teacherHistoryTasks || total == 0) {
            return  new PageBean<>(200, "查询列表为空");
        }
        return new PageBean<>(200, "Success", teacherHistoryTasks);
    }

    @Override
    public PageBean<TeacherHistoryTask> queryHistoryTaskByGroupId(Integer groupId, Integer courseId, Integer pageNum, Integer pageSize) {
        if (pageNum==null || pageSize==null){
            pageNum = 1;
            pageSize = 10;
        }
        PageBean pageBean = new PageBean();
        pageBean.setPageNum(pageNum);
        pageBean.setPageSize(pageSize);
        PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        Map<String,Object> map = new HashMap<>();
        map.put("groupId",groupId);
        map.put("courseId",courseId);
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        List<TeacherHistoryTask> teacherHistoryTaskList = teacherScoreDao.queryHistoryTaskByGroupId(map);
        int total = teacherHistoryTaskList.size();

        if (null == teacherHistoryTaskList || total == 0) {
            return  new PageBean<>(200, "查询列表为空");
        }
        return new PageBean<>(200, "Success",teacherHistoryTaskList);
    }

    @Override
    public Page updateStudentCourseByGroupId(float overAllScore, String comment, Integer groupId,Integer responseTeacher) {
        //当前登录教师id
        int teacherId = LocalJwt.getCurrentUserId(request);
        if (teacherId == responseTeacher){
            Date submitTime = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Map<String,Object> map = new HashMap<>();
            map.put("overAllScore",overAllScore);
            map.put("comment",comment);
            map.put("groupId",groupId);
            map.put("submitTime",sdf.format(submitTime));
            int count = teacherScoreDao.updateStudentCourseByGroupId(map);
            if (count > 0){
                return Page.SUCCESS;
            }else {
                return Page.FAIL;
            }
        }else {
            return new Page(201,"当前用户没有该权限");
        }
    }
}
